package com.hxx;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.output.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Base64;

@RestController
public class AiController {


    @Value("classpath:hxx.png")
    private Resource resource;

    @Autowired
    private ChatLanguageModel chatLanguageModel;

    @GetMapping("/pic")
    public String readPicContent() throws IOException {
        //资源转为Base64
        byte[] baseArray = resource.getContentAsByteArray();
        //base64编码
        String encodeToString = Base64.getEncoder().encodeToString(baseArray);
        //读取图片
        UserMessage userMessage = UserMessage.from(TextContent.from("从以下图片中获取9.30号的上证指数"),
                ImageContent.from(encodeToString, "images/png"));
        //调用模型输出内容
        Response<AiMessage> generate = chatLanguageModel.generate(userMessage);
        return generate.content().text();
    }
}


























